$(function () {
  let form = layui.form;
  let layer = layui.layer;
  //邮箱正则表达式
  form.verify({
    email: [/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/],
  });
  //获取用户ID
  let id = new URLSearchParams(location.search).get('id');
  //   console.log(id);
  //获取用户信息
  getUserInfo();
  function getUserInfo() {
    $.ajax({
      url: 'admin/users/' + id,
      success: function (res) {
        // console.log(res);
        if (res.status === 0) {
          form.val('editForm', res.data);
        } else {
          layer.msg(res.message);
        }
      },
    });
  }

  $('#editForm').submit(function (e) {
    e.preventDefault();
    let data = $(this).serialize();
    // console.log(data);
    $.ajax({
      url: 'admin/users',
      type: 'PUT',
      data,
      success: function (res) {
        if (res.status !== 0) {
          return layer.msg(res.message);
        }
        layer.msg(res.message);
      },
    });
  });
});
